home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-04 | 17.7 KB | 617 lines |
- /*
- * Title: Calendar
- * Type: Applet
- * Source: Calendar.java
- * Application Description: Display a calendar window with the
- * month/year selected by the user. When a date is selected,
- * a scrolling text box is displayed at the top along with the
- * event information for that date. Event information is input
- * and extracted from HTML. The format for the "date:event"
- * HTML is the following:
- *
- * param name=EVENT1 value="01/23/96:January 23, 1996 - Bake Sale"
- * param name=EVENT2 value="01/30/96:January 30, 1996 - Ski Vacation"
- *
- * NOTE: The date and event string information are separated by a
- * colon (:); the format for the date MUST be MM/DD/YY.
- *
- * Symantec Corporation.
- */
-
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.Font;
- import java.util.Date;
- import java.util.Hashtable;
- import java.lang.String;
-
- /**
- * The main applet class
- */
-
- public class Calendar extends Applet implements Runnable
- {
- Button myButton[]; // The calendar date buttons
- Image bannerText; // The banner text display object
- MyCanvas textCanvas; // The banner text display object
- boolean loaded = true;
- Graphics textGC;
-
- //TextField date; // Date display text field
- int thismonth; // App. variable
- int thisyear; // App. variable
- int days; // App. variable
- int dayselected; // App. variable
- Date dateSelected; // User-selected Date object
- int buttonShift; // Used to determine what day the
- // 1st of the month falls on
-
- Hashtable eventHash; // Used to track date/event combinations
-
- //Thread related variables.
- Thread bannerThread; // The main banner thread
- int maxWidth; // The width of the banner window
- int maxHeight; // The height of the banner window
- int bannerHeight = 20; // The height of the banner text
- public String message; // The string displayed on the banner
- int msgX = 0; // X-coordinate of the banner string
- int msgY = 0; // Y-coordinate of the banner string
- int msgWidth = 0; // This is the width of the message in pixels
- public Dimension lastS = new Dimension(1, 1);
- public Font msgFont; // The font used in the banner
- static boolean loadedtext = true;
-
- /**
- * Set up the three main panels (top,middle,lower) along with the
- * UI components (calendar buttons, etc.)
- */
-
- public void init()
- {
- message = "Welcome to the Java Calendar!!!";
-
- eventHash = new Hashtable();
- getParamData();
-
- setLayout(new BorderLayout());
-
- //Set up the banner...
- bannerText = createImage(300,20);
- textGC = bannerText.getGraphics();
- textGC.setColor(Color.blue);
- textGC.fillRect(4, 4, 300, 20);
- textGC.setColor(Color.white);
-
- textCanvas = new MyCanvas(bannerText);
- textCanvas.resize(300, 25);
-
- //Set up the center (calendar) panel...
- Label sunday, monday, tuesday, wednesday, thursday, friday, saturday;
- sunday = new Label("Sun");
- monday = new Label("Mon");
- tuesday = new Label("Tue");
- wednesday = new Label("Wed");
- thursday = new Label("Thu");
- friday = new Label("Fri");
- saturday = new Label("Sat");
-
- Panel datePanel = new Panel();
- datePanel.setLayout(new GridLayout(7, 7));
-
- datePanel.add(sunday);
- datePanel.add(monday);
- datePanel.add(tuesday);
- datePanel.add(wednesday);
- datePanel.add(thursday);
- datePanel.add(friday);
- datePanel.add(saturday);
-
- myButton=new Button[42];
- for(int i = 0; i < 42; i++)
- {
- myButton[i] = new Button("");
- datePanel.add(myButton[i]);
- }
-
- //{{INIT_CONTROLS
- setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
- addNotify();
- resize(430,270);
- selPanel = new java.awt.Panel();
- selPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
- selPanel.reshape(3,76,321,54);
- selPanel.setFont(new Font("Dialog", Font.PLAIN, 12));
- add(selPanel);
- leftButton = new java.awt.Button("<<<");
- leftButton.reshape(18,18,63,26);
- selPanel.add(leftButton);
- date = new java.awt.TextField(15);
- date.reshape(94,18,130,26);
- selPanel.add(date);
- rightButton = new java.awt.Button(">>>");
- rightButton.reshape(237,18,63,26);
- selPanel.add(rightButton);
- //}}
-
- date.setEditable(false);
-
- add("North", textCanvas);
- add("Center", datePanel);
-
- textCanvas.resize(299, 25);
- datePanel.resize(299, 225);
- selPanel.resize(299, 25);
- }
-
- /**
- * Find out today's date and set up the calendar to display the correct
- * month and year
- */
-
- public void start()
- {
- Date todaydate = new Date();
- setMonthString(todaydate);
- setCalendar(todaydate);
-
- //Establish the font size and characteristics for the banner
- CreateBannerParams();
-
- bannerThread = new Thread(this);
- bannerThread.start();
- }
-
-
- /**
- * The paint function is used to draw the text string which is
- * displayed along the top of the calendar.
- */
-
- public void paint(Graphics g)
- {
- g.drawImage(bannerText, 0, 0, this);
- }
-
- /**
- * Handle the events of the date-selection buttons as well as the
- * buttons corresponding to specific days.
- */
-
- public boolean action(Event e, Object arg)
- {
- if(e.target instanceof Button)
- {
- if(">>>" == arg)
- {
- incrementCalendar();
- } else if("<<<" == arg)
- {
- decrementCalendar();
- } else if (arg != "")
- {
- Integer tempInt = Integer.valueOf((String)arg);
- dayselected = tempInt.intValue();
- dateSelected = getDateSelected();
- displayBannerText(dateSelected);
- repaint();
- }
- }
- return false;
- }
-
-
- /**
- * Given a Date, set up the calendar elements to display the
- * appropriate dates on the appropriate calendar buttons.
- */
-
- private void setCalendar(Date dt)
- {
- //clear calendar
- for(int i = 0; i < 42; i++)
- {
- myButton[i].setLabel("");
- }
-
- int thisMo = dt.getMonth();
- int thisDy = dt.getDay();
- int thisYr = dt.getYear();
-
- //Find the day of the week of the first
- Date firstday = new Date(thisYr, thisMo, 1);
- int dayoffirst = firstday.getDay();
- buttonShift = dayoffirst;
-
- int dayspermonth = getDaysPerMonth(thisMo);
- for(int i = 1; i < dayspermonth+1; i++)
- {
- myButton[i+buttonShift-1].setLabel(Integer.toString(i));
- }
- }
-
-
- /**
- * Given a Date, set the text field to display it as a
- * string containing the month and the year.
- */
-
- private void setMonthString(Date dt)
- {
- thismonth = dt.getMonth();
- thisyear = dt.getYear();
-
- String monthstring;
-
- switch(thismonth)
- {
- case 0:
- monthstring = "January";
- break;
- case 1:
- monthstring = "February";
- break;
- case 2:
- monthstring = "March";
- break;
- case 3:
- monthstring = "April";
- break;
- case 4:
- monthstring = "May";
- break;
- case 5:
- monthstring = "June";
- break;
- case 6:
- monthstring = "July";
- break;
- case 7:
- monthstring = "August";
- break;
- case 8:
- monthstring = "September";
- break;
- case 9:
- monthstring = "October";
- break;
- case 10:
- monthstring = "November";
- break;
- case 11:
- monthstring = "December";
- break;
- default:
- monthstring = "Month";
- break;
- }
- String temp = (monthstring +" ");
- temp = temp + (Integer.toString(1900 + thisyear));
- date.setText(temp);
- }
-
-
- /**
- * Increments the calendar (both Mo/Yr text field and date buttons)
- * to the next sequential month/year combination.
- */
-
- private void incrementCalendar()
- {
- thismonth += 1;
- if(thismonth > 12)
- {
- thismonth = 0;
- thisyear += 1;
- }
- Date incDate = new Date(thisyear, thismonth, 1);
- setMonthString(incDate);
- setCalendar(incDate);
- }
-
- /**
- * Decrements the calendar (both Mo/Yr text field and date buttons)
- * to the previous sequential month/year combination.
- */
-
- private void decrementCalendar()
- {
- thismonth -= 1;
- if(thismonth < 0)
- {
- thismonth = 11;
- thisyear -= 1;
- }
- Date decDate = new Date(thisyear, thismonth, 1);
- setMonthString(decDate);
- setCalendar(decDate);
- }
-
- /**
- * Given a month (represented as an integer), return the
- * number of days in that month (corrects for leap years).
- */
-
- private int getDaysPerMonth(int m)
- {
- switch(m)
- {
- case 0:
- days = 31;
- break;
-
- case 1:
- //Correct for a leap year
- int tempYear = 1900 + thisyear;
- if((tempYear % 4) == 0) // Could be leap
- {
- if((tempYear % 100) == 0 && // Centuries aren't
- (tempYear % 400) != 0) // Except every 4th
- days = 28;
- else
- days = 29;
- break;
- }
- days = 28;
- break;
-
- case 2:
- days = 31;
- break;
-
- case 3:
- days = 30;
- break;
- case 4:
- days = 31;
- break;
-
- case 5:
- days = 30;
- break;
-
- case 6:
- days = 31;
- break;
-
- case 7:
- days = 31;
- break;
-
- case 8:
- days = 30;
- break;
-
- case 9:
- days = 31;
- break;
-
- case 10:
- days = 30;
- break;
-
- case 11:
- days = 31;
- break;
-
- default:
- days = 31;
- break;
- }
- return days;
- }
-
- /**
- * return the date that was selected by pushing a button
- * on the calendar
- */
-
- private Date getDateSelected()
- {
- dateSelected = new Date(thisyear, thismonth, dayselected);
- return dateSelected;
- }
-
- /**
- * Given a date, set the banner text message
- */
-
- private void displayBannerText(Date dt)
- {
- message = (String)eventHash.get(dt);
-
- if(message == null)
- {
- int tempMo = (dt.getMonth()) + 1;
- int tempYr = (dt.getYear());
- int tempDy = (dt.getDate());
- message = "You selected ";
- message += Integer.toString(tempMo);
- message += "/" + Integer.toString(tempDy);
- message += "/" + Integer.toString(tempYr);
- }
- CreateBannerParams();
- }
-
- /**
- * An initialization function designed to get parameter info,
- */
-
- private void getParamData()
- {
- String param = null;
- boolean datapresent = true;
- int i = 1;
-
- while(datapresent)
- {
- try
- {
- param = getParameter("EVENT"+ Integer.toString(i));
- } catch(Exception e)
- {
- System.out.println(e);
- }
-
- if(param == null)
- {
- datapresent = false;
- } else
- {
- i += 1;
- parseHashData(param);
- }
- }
- }
-
-
- /**
- * A small utility to take the param info, turn it into program-friendly
- * classes and strings and then put all that info into a hash table.
- * The string fed to parseHashData is the raw param string from the HTML.
- */
-
- private void parseHashData(String str)
- {
- //Separate the param string into its date/event components
- int sep = str.indexOf(':');
- String evt = str.substring(sep+1);
- String evtdate = str.substring(0, sep);
-
- //make the evtdate into a date class format (YY/MM/DD). The input
- //string is in the U.S.-style MM/DD/YY format.
-
- int sel1 = evtdate.indexOf('/');
- int sel2 = evtdate.lastIndexOf('/');
-
- String mo = evtdate.substring(0, sel1);
- String dy = evtdate.substring(sel1+1, sel2);
- String yr = evtdate.substring(sel2+1);
-
- int intMo = Integer.parseInt(mo);
- int intDay = Integer.parseInt(dy);
- int intYr = Integer.parseInt(yr);
-
- Date newDate = new Date(intYr, intMo-1, intDay);
-
- // Put the evt and evtdate info into a Hashtable
- eventHash.put(newDate, evt);
- }
-
- /**
- * The main thread event function
- */
-
- public void run()
- {
- Thread me = Thread.currentThread();
- me.setPriority(Thread.MIN_PRIORITY);
-
- repaint();
-
- while(loadedtext)
- {
- drawText();
- nextPos();
-
- textCanvas.repaint();
-
- try
- {
- Thread.sleep(150);
- } catch(InterruptedException e)
- {}
- }
- }
-
- /**
- * Draws the appropriate text string onto the (invisible) textGC
- * graphics context. Used prior to a repaint request.
- */
-
- public synchronized void drawText()
- {
- if(loadedtext)
- {
- //following code for banner...
- textGC.setColor(Color.blue);
- textGC.fillRect(0, 0, 300, bannerHeight);
- textGC.setColor(Color.white);
- textGC.setFont(msgFont);
- textGC.drawString(message, msgX, msgY);
- }
- }
-
- /**
- * Scrolls the banner text one "position" to the left.
- */
-
- public synchronized void nextPos()
- {
- msgX -= 10;
- if((msgX + msgWidth) < 0)
- {
- msgX = lastS.width;
- }
- repaint();
- }
-
-
- /**
- * Create the original banner parameters (font size, etc.)
- */
-
- public void CreateBannerParams()
- {
- int w = 300;
- int h = 20;
- lastS.width = w;
- lastS.height = h;
-
- int refH = 14;
- Font tf = new Font("TimesRoman", Font.BOLD, refH);
- textGC.setFont(tf);
- FontMetrics tfm = textGC.getFontMetrics(tf);
- int fh = tfm.getHeight();
- fh = refH*(h-2)/fh;
- msgFont = new Font("TimesRoman", Font.BOLD, fh);
- FontMetrics fm = textGC.getFontMetrics(msgFont);
- fh = fm.getHeight();
- msgX = w;
- msgY = ((h-fh) >> 1) + fm.getAscent();
- msgWidth = fm.stringWidth(message);
- }
- //{{DECLARE_CONTROLS
- java.awt.Panel selPanel;
- java.awt.Button leftButton;
- java.awt.TextField date;
- java.awt.Button rightButton;
- //}}
-
- }
-
-
-
- /**
- * Subclasses Canvas object which allows display of running (banner) text
- */
-
- class MyCanvas extends Canvas
- {
- Image im;
-
- MyCanvas(Image img)
- {
- im = img;
- }
-
- public void paint(Graphics g)
- {
- g.drawImage(im, 0, 0, this);
- }
-
- public void update(Graphics g)
- {
- paint(g);
- }
- }
-